package cn.hnkjxy.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.hnkjxy.po.User;
import cn.hnkjxy.service.OrderService;
import cn.hnkjxy.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String toLogin() {
		return "login";
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String toRegister() {
		return "register";
	}
	
	@RequestMapping(value="admin/toIndex")
	public String toIndex() {
		return "admin/index";
	}
	
	@RequestMapping(value="/admin/toMianUser")
	public String toMianUser() {
		return "admin/user";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(String username, String password, Model model, HttpSession session) {
		User user = this.userService.findUser(username, password);
		if (user != null) {
			session.setAttribute("user", user);
			if(user.getRole().equals("普通会员")) {
				return "redirect:findAllProduct";
			} else {
				return "admin/index";
			}
		}
		model.addAttribute("msg", "用户名或密码有误");
		return "login";
	}

	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:findAllProduct";
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String register(String username, String password, String usertel, String gender, String role) {
		int row = this.userService.addUser(username, password, usertel, gender, role);
		if (row > 0) {
			return "login";
		} else {
			return "redirect:findAllProduct";
		}

	}

	@RequestMapping(value = "/PersonalCenter")
	public String PersonalCenter(HttpSession session) {
		User user1 = (User) session.getAttribute("user");
		User user = userService.findUserById(user1.getUsid());
		session.setAttribute("user", user);
		return "revice";
	}

	@RequestMapping("/reviceUser")
	public String reviceUser(User user, HttpSession session, Model model) {
		User user1 = (User) session.getAttribute("user");
		user.setUsid(user1.getUsid());
		Integer ro = this.userService.reviceUser(user);
		if (ro == 1) {
			model.addAttribute("msg", "修改成功");
		} else {
			model.addAttribute("msg", "修改失败");
		}
		return "revicesuccess";
	}
	
	@RequestMapping(value="/admin/findAllUser")
	public String findAllUser(Model model) {
		List<User> users = this.userService.findAllUser();
		model.addAttribute("users", users);
		return "forward:toMianUser";
	}
	
	@RequestMapping("/admin/deleteUser")
	@ResponseBody
	public String deleteUser(Integer usid) {
		Integer row1 = this.orderService.deleteOrderByUser(usid);
		Integer row2 = this.userService.deleteUser(usid);
		if (row2 > 0) {
			return "ok";
		}else {
			return "no";
		}
	}
	
	@RequestMapping(value="/admin/getUserById",method=RequestMethod.GET)
	@ResponseBody
	public User getUserById(Integer usid) {
		User user = this.userService.findUserById(usid);
		return user;
	}
	
	@RequestMapping(value="/admin/preservationModify")
	@ResponseBody
	public String preservationModify(User user) {
		Integer row = this.userService.reviceUser(user);
		if(row > 0) {
			return "ok";
		}else {
			return "no";
		}
	}
	
	@RequestMapping(value="admin/findUserByCondition")
	public String findUserByCondition(String username,String rname,String usertel,String gender,String usernumber,Model model) {
		User user1 = new User();
		user1.setGender(gender);
		user1.setUsername(username);
		user1.setUsernumber(usernumber);
		user1.setUsertel(usertel);
		user1.setRname(rname);
		List<User> users = this.userService.findUserByCondition(user1);
		model.addAttribute("users", users);
		return "forward:toMianUser";
	}
	@RequestMapping(value="/admin/addUser")
	@ResponseBody
	public String addUser(User user) {
		Integer row = this.userService.addUser2(user);
		if(row > 0) {
			return "ok";
		}else {
			return "no";
		}
	}
	
	@RequestMapping(value="/admin/verification")
	@ResponseBody
	public Integer verification(String username) {
		Integer usid = this.userService.findUserNameById(username);
		return usid;
	}
	
	@RequestMapping(value="/findPassword")
	@ResponseBody
	public String findPassword(Integer usid,HttpSession session) {
		User user=(User) session.getAttribute("user");
		User user2= this.userService.findPassword(user.getUsid());	
		return JSON.toJSONString(user2);
	}
}
